<template>
  <div class="layout">
    <el-container>
      <el-aside
        :style="{width: isCollapse ? '65px' : '220px', borderRight: '1px solid #f2f6fc', height: '100vh', position: 'fixed'}"
      >
        <div class="el-header" style="line-height: 60px;">{{userinfo.username}}</div>
        <el-menu
          class="el-menu-vertical-demo"
          :collapse="isCollapse"
          :unique-opened="uniqueOpened"
          router
          v-loading="loading"
        >
          <el-submenu :index="menu.name" v-for="(menu, index) in menus" :key="index">
            <template slot="title">
              <i v-if="menu.icon" :class="menu.icon"></i>
              <span slot="title">{{menu.name}}</span>
            </template>
            <el-menu-item-group>
              <span slot="title">{{menu.name}}</span>
              <el-menu-item
                v-for="(v, i) in menu.children"
                :key="i"
                :index="`/${v.module}/${v.controller}/${v.action}`"
              >{{v.name}}</el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container :style="{marginLeft: isCollapse ? '65px' : '220px'}">
        <el-header :style="{padding: 0, position: 'fixed', width: getWidth, zIndex: 8}">
          <a href="javascript:;" @click="isCollapse = !isCollapse">
            <i class="el-icon-more"></i>
          </a>
          <div class="pull-right">
            <a href="javascript:;" title="检测更新">
              <i class="el-icon-refresh"></i>
            </a>
            <a href="javascript:;" title="清除缓存">
              <i class="el-icon-delete"></i>
            </a>
            <div style="display: inline-block; position: relative;">
              <a href="javascript:;" @click="showUserInfo = !showUserInfo">
                <img class="avatar" :src="userinfo.avatar" alt="avatar" />
                <span>{{userinfo.username}}</span>
              </a>
              <div class="profile" v-show="showUserInfo">
                <div class="top">
                  <img :src="userinfo.avatar" alt="avatar" />
                  <p class="username">{{userinfo.username}}</p>
                  <p>{{userinfo.last_login_time | moment('YYYY-MM-DD')}}</p>
                </div>
                <div class="footer">
                  <el-button type="danger" @click="logout">
                    <i class="el-icon-s-unfold"></i>
                    <span>注销</span>
                  </el-button>
                </div>
              </div>
            </div>
          </div>
        </el-header>
        <el-main style="margin-top: 60px;">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'layout',
  data () {
    return {
      loading: true,
      isCollapse: false,
      showUserInfo: false,
      uniqueOpened: true,
      menus: [],
      userinfo: {}
    }
  },
  mounted () {
    // 获取登录信息
    this.userinfo = JSON.parse(this.$cookie.get('userinfo'))
    // 请求获取菜单列表
    this.$axios.get('/admin/menus')
      .then(response => {
        this.loading = false
        if (response.data.err_code) {
          this.$message.warning(response.data.message)
        } else {
          this.menus = response.data
        }
      })
      .catch((e) => {
        this.loading = false
        this.$message.error('获取菜单失败')
      })
  },
  methods: {
    logout () {
      this.$confirm('确认退出系统么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$cookie.delete('userinfo')
          this.$message({
            type: 'success',
            message: '退出系统',
            duration: 1000,
            onClose: () => {
              window.location.href = '/login'
            }
          })
        }).catch(() => { })
    }
  },

  computed: {
    getWidth () {
      return this.isCollapse ? 'calc(100% - 65px)' : 'calc(100% - 220px)'
    }
  }
}
</script>
